<?php 



class Model_Payroll_Shopgroupbonus extends Zend_Db_Table_Abstract {

	protected $_name = 'shop_group_bonus';
	
	public function getShopbonus( $idBonus){
	
		$row = $this->fetchRow("`id_bonus` = ". $idBonus);
		if(!$row) return false;
		return $row->toArray();
	
	}
	
	public function addShopbonus( $idShop , $typeBonus , $detailBonus , $amtTotalBonus , $amtCashBonus , $amtBankBonus , $amtBonusSuper , $periodBonusBegin , $periodBonusEnd , $payDate , $payType , $holdToDate = null , $paidBonus = null, $paidDate = null, $cancelBonus = null, $paidAmtCash = null, $paidAmtBank = null, $paidAmtSuper= null){
	
		$data = array(
					
				"id_shop" =>  $idShop ,
				"type_bonus" =>  $typeBonus ,
				"detail_bonus" =>  $detailBonus ,
				"amt_total_bonus" =>  $amtTotalBonus ,
				"amt_cash_bonus" =>  $amtCashBonus ,
				"amt_bank_bonus" =>  $amtBankBonus ,
				"amt_bonus_super" =>  $amtBonusSuper ,
				"period_bonus_begin" =>  $periodBonusBegin ,
				"Period_bonus_end" =>  $periodBonusEnd ,
				"pay_date" =>  $payDate ,
				"pay_type" =>  $payType ,
				"hold_to_date" =>  $holdToDate ,
				"paid_bonus" =>  $paidBonus ,
				"paid_date" =>  $paidDate ,
				"cancel_bonus" =>  $cancelBonus ,
				"paid_amt_cash" =>  $paidAmtCash ,
				"paid_amt_bank" =>  $paidAmtBank ,
				"paid_amt_super" =>  $paidAmtSuper
					
	
		);
		$this->insert($data);
	
	}
	
	public function newShopbonus($idShop , $typeBonus , $detailBonus , $amtTotalBonus , $amtCashBonus , $amtBankBonus , $amtBonusSuper , $periodBonusBegin , $periodBonusEnd , $payDate , $payType , $holdToDate,$holdReason,$shopCode = null,$signStaff = null){
	
		$data = array(
					
				"id_shop" =>  $idShop ,
				"type_bonus" =>  $typeBonus ,
				"detail_bonus" =>  $detailBonus ,
				"amt_total_bonus" =>  $amtTotalBonus ,
				"amt_cash_bonus" =>  $amtCashBonus ,
				"amt_bank_bonus" =>  $amtBankBonus ,
				"amt_bonus_super" =>  $amtBonusSuper ,
				"period_bonus_begin" =>  $periodBonusBegin ,
				"Period_bonus_end" =>  $periodBonusEnd ,
				"pay_date" =>  $payDate ,
				"pay_type" =>  $payType ,
				"hold_to_date" =>  $holdToDate,
				"hold_reason" =>  $holdReason,
				"shop_code" => $shopCode,
				"signed_by" => $signStaff
	
		);
		$this->insert($data);
	
	
	}
	
	
	
	public function changePayDate($idBonus,$payDate,$holdToDate){
		$data = array(
					
				"pay_date" =>  $payDate ,
				"hold_to_date" =>  $holdToDate
		);
			
		$this->update($data,"`id_bonus` = ". $idBonus);
	
	}
	public function cancelShopBonus($idBonus,$comment = null){
		$data = array(
				"cancel_bonus" =>  Model_DatetimeHelper::dateToday(),
				"release_comment" => $comment
		);
			
		$this->update($data,"`id_bonus` = ". $idBonus);
	
	}
	
	public function updateShopbonus(  $idBonus ,  $idShop , $typeBonus , $detailBonus , $amtTotalBonus , $amtCashBonus , $amtBankBonus , $amtBonusSuper , $periodBonusBegin , $periodBonusEnd , $payDate , $payType , $holdToDate , $paidBonus , $paidDate , $cancelBonus , $paidAmtCash , $paidAmtBank , $paidAmtSuper){
		$data = array(
					
				"id_shop" =>  $idShop ,
				"type_bonus" =>  $typeBonus ,
				"detail_bonus" =>  $detailBonus ,
				"amt_total_bonus" =>  $amtTotalBonus ,
				"amt_cash_bonus" =>  $amtCashBonus ,
				"amt_bank_bonus" =>  $amtBankBonus ,
				"amt_bonus_super" =>  $amtBonusSuper ,
				"period_bonus_begin" =>  $periodBonusBegin ,
				"Period_bonus_end" =>  $periodBonusEnd ,
				"pay_date" =>  $payDate ,
				"pay_type" =>  $payType ,
				"hold_to_date" =>  $holdToDate ,
				"paid_bonus" =>  $paidBonus ,
				"paid_date" =>  $paidDate ,
				"cancel_bonus" =>  $cancelBonus ,
				"paid_amt_cash" =>  $paidAmtCash ,
				"paid_amt_bank" =>  $paidAmtBank ,
				"paid_amt_super" =>  $paidAmtSuper
	
		);
			
		$this->update($data,"`id_bonus` = ". $idBonus);
	}
	/**
	 * For update adj stock lost
	 * @version 2014-09-09
	 * @author Leo
	 * @param unknown_type $idBonus
	 * @param unknown_type $amtTotalBonus
	 * @param unknown_type $amtCashBonus
	 */
	public function updateAdjStockLost(  $idBonus ,  $amtTotalBonus , $amtCashBonus ){
		$data = array(

				"amt_total_bonus" =>  $amtTotalBonus ,
				"amt_cash_bonus" =>  $amtCashBonus ,
	
	
		);
			
		$this->update($data,"`id_bonus` = ". $idBonus);
	}
	
	public function deleteShopbonus( $idBonus){
	
		$this->delete("`id_bonus` = ". $idBonus);
	
	}
	
	public function listAll(){
	
		$rows =$this->fetchAll("1","id_bonus DESC ");
		if(!$rows) return false;
		return $rows->toArray();
	
	}
	
	public function listBonusByPayDate($dateBegin = null){
	
		if($dateBegin==""){
			$dateBegin = Model_DatetimeHelper::dateToday();
		}
	
		$mBegin = Model_DatetimeHelper::getFirstDayOfTheMonth($dateBegin);
		$mEnd = Model_DatetimeHelper::getLastDayOfTheMonth($dateBegin);

		
	
		$whereStr = "`pay_date` >= '{$mBegin}' AND `pay_date` <= '{$mEnd}' AND `hold_to_date` = '0000-00-00' AND `cancel_bonus` IS NULL ";

	
		$rows =$this->fetchAll($whereStr,"id_shop ASC ");
		if(empty($rows)) return false;
		return $rows->toArray();
	
	
	}
	public function listReleasedBonus($idShop){
	
	$whereStr = "`id_shop` = {$idShop} AND `hold_to_date` = '0000-00-00' AND `type_bonus` < 4 ";
	$rows =$this->fetchAll($whereStr,"pay_date ASC ");
	if(empty($rows)) return false;
	return $rows->toArray();
	
	}
	public function listAllHoldBonus(){
	
	$whereStr = "`type_bonus` < 4 AND `hold_to_date` != '0000-00-00' AND `cancel_bonus` IS NULL ";
	
	$rows =$this->fetchAll($whereStr,"id_shop ASC ");
	if(empty($rows)) return false;
	return $rows->toArray();
	
	
	}
	
	public function bonusPaid($idBonus,$paidDate){
	$data = array(
			"paid_date" =>  $paidDate
	);
		
	$this->update($data,"`id_bonus` = ". $idBonus);
	
	}
	
	public function updateHoldReason($idBonus){
		$data = array(
				"hold_reason" =>  1
		);
	
		$this->update($data,"`id_bonus` = ". $idBonus);
	
	}
	
	public function getStockLostByShop($shopCode,$dateBegin,$dateEnd,$stkLostType = 4){
	$whereStr = "`shop_code` LIKE '{$shopCode}' AND `period_bonus_begin` = '{$dateBegin}' AND `Period_bonus_end` = '{$dateEnd}' AND `type_bonus` = {$stkLostType}";
	$rows =$this->fetchRow($whereStr);
	if(!$rows) return false;
	return $rows->toArray();
	
	}
	/**
	 * For get stock lost by type
	 * @version 2014-08-22
	 * @author Leo
	 * @param unknown_type $shopCode
	 * @param unknown_type $dateBegin
	 * @param unknown_type $dateEnd
	 * @param unknown_type $stkLostType
	 */
	public function getStockLostByShopByType($shopCode,$dateBegin,$dateEnd,$stkLostType){
		$whereStr = "`shop_code` LIKE '{$shopCode}' AND `period_bonus_begin` = '{$dateBegin}' AND `Period_bonus_end` = '{$dateEnd}' AND `type_bonus` = {$stkLostType}";
		$rows =$this->fetchRow($whereStr);
		if(!$rows) return false;
		return $rows->toArray();
	
	}
	
	public function getStockLostByShopIdByType($idShop,$dateBegin,$dateEnd,$stkLostType){
		$whereStr = "`id_shop` = {$idShop} AND `period_bonus_begin` = '{$dateBegin}' AND `Period_bonus_end` = '{$dateEnd}' AND `type_bonus` = {$stkLostType}";
		$rows =$this->fetchRow($whereStr);
		if(!$rows) return false;
		return $rows->toArray();
	
	}
	
	
	public function getAllStockLostByShopByType($shopCode,$dateBegin,$dateEnd,$stkLostType){
		$whereStr = "`shop_code` LIKE '{$shopCode}' AND `period_bonus_begin` = '{$dateBegin}' AND `Period_bonus_end` = '{$dateEnd}' AND `type_bonus` = {$stkLostType}";
		$rows =$this->fetchAll($whereStr);
		if(!$rows) return false;
		return $rows->toArray();
	
	}
	
	
	
	/**
	 * For get all other detuction lost
	 * @author Leo
	 * @version 2014-10-13
	 */
	public function getAllOtherLost(){
		$whereStr = "`type_bonus` = 10";
	
		$rows =$this->fetchAll($whereStr,"hold_reason DESC ");
		if(empty($rows)) return false;
		return $rows->toArray();
	}
	
	public function releaseBonus($idBonus,$payDate,$releaseComment){
	
		$data = array(
				"pay_date" =>  $payDate ,
				"hold_to_date" =>  '0000-00-00' ,
				"release_comment" => $releaseComment
				);
				$this->update($data,"`id_bonus` = ". $idBonus);
		}
	/**
	 * For the New Shop Bonus, when Staff inpu the password , record the staff name 
	 * @param unknown_type $idBonus
	 * @param unknown_type $idStaff
	 */	
	public function signBonus($idBonus,$idStaff){
		
		$bList = self::getShopbonus($idBonus);
		$signList = $bList['signed_by'];
		
		if($signList!=""){
		
			$arrStaff = explode("|", $signList);
			if(in_array($idStaff, $arrStaff)){
				
				return false;
			}
			else{
				$signList .= $idStaff."|";
			}		
		}
		else{
			$signList = $idStaff."|";
			
		}
		
		
		$data = array(
			"signed_by" =>$signList 			
		);
		$this->update($data,"`id_bonus` = ". $idBonus);	

		return true;
		
	}	
}
?>